package com.kye.express.business.invoice_online.history;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.invoice_online.detail.InvoiceDetailActivity;
import com.kye.express.business.president_online.PresidentOnlineListener;
import com.kye.express.databinding.ItemInvoiceHistoryBinding;
import com.kye.express.databinding.ItemPresidentOnlineListBinding;
import com.kye.express.utils.DateTimeUtils;
import com.kye.express.utils.StringUtils;
import com.kye.model.bean.invoice_online.FindInvoicingRecordsResultBean;
import com.kye.model.bean.president_online.PresidentOnlineListBean;

import io.reactivex.functions.Consumer;

class InvoiceHistoryListItemViewBinder extends me.drakeet.multitype.ItemViewBinder<FindInvoicingRecordsResultBean, InvoiceHistoryListItemViewBinder.ViewHolder> {

    private Context mContext;

    public InvoiceHistoryListItemViewBinder(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_invoice_history, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull FindInvoicingRecordsResultBean item) {
        holder.bindData(item);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private FindInvoicingRecordsResultBean data;
        ItemInvoiceHistoryBinding dataBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
        }


        public void bindData(FindInvoicingRecordsResultBean item) {
            data = item;
            if (data != null) {
                dataBinding.setData(data);
                RxView.clicks(dataBinding.getRoot())
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {
                            @Override
                            public void accept(Object o) throws Exception {
                                if (!TextUtils.isEmpty(data.getGroupCode())) {
                                    InvoiceDetailActivity.startActivity(mContext, data.getGroupCode());
                                }
                            }
                        });
            }
        }
    }
}
